package com.bw.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.finance.conf.ResponseResult;
import com.bw.finance.controller.EbankInvestorController;
import com.bw.finance.eneity.user.EbankBorrower;
import com.bw.finance.eneity.user.EbankInvestor;
import com.bw.finance.eneity.user.res.ZhuceLogin;
import com.bw.finance.mapper.EbankInvestorMapper;
import com.bw.finance.service.IEbankBorrowerService;
import com.bw.finance.service.IEbankInvestorService;
import com.bw.finance.utils.MD5Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.security.PrivateKey;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhengqian
 * @since 2020-08-17
 */
@Service
public class EbankInvestorServiceImpl extends ServiceImpl<EbankInvestorMapper, EbankInvestor> implements IEbankInvestorService {

    @Resource
    private IEbankInvestorService iEbankInvestorService;

    @Autowired
    private IEbankBorrowerService borrowerService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final Logger log= LoggerFactory.getLogger(EbankInvestorController.class);

    @Override
    public ResponseResult invlist(int pagenum, int pagesize, EbankInvestor ebankInvestor) {
        ResponseResult result = ResponseResult.FAIL();
        try {
            PageHelper.startPage(pagenum,pagesize);
            System.out.println("开始列表查询");
            QueryWrapper<EbankInvestor> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().orderByDesc(EbankInvestor::getRegTime);
            if(ebankInvestor!=null){
                if(ebankInvestor.getFullName()!=null){
                    queryWrapper.lambda().like(EbankInvestor::getFullName,ebankInvestor.getFullName());
                }
            }
            List<EbankInvestor> list01 = iEbankInvestorService.list(queryWrapper);
            PageInfo<EbankInvestor> pageInfo = new PageInfo<>(list01);
            result.setResult(pageInfo);
            result.setSuccess(true);
            result.setMessage("成功");
        }catch (Exception e){
            System.out.println(e.toString());
            result.setMessage(e.toString());
        }
        System.out.println("列表查询结束");
        return result;
    }

    @Override
    public ResponseResult update01(int id, int val) {
        ResponseResult result = ResponseResult.FAIL();
        try {
            System.out.println("开始状态修改");
            EbankInvestor byId = iEbankInvestorService.getById(id);
            byId.setAuthStatus(val);
            iEbankInvestorService.saveOrUpdate(byId);
            result.setSuccess(true);
            result.setMessage("成功");
        }catch (Exception e){
            System.out.println(e.toString());
            result.setMessage(e.toString());
        }
        System.out.println("状态修改结束");
        return result;
    }

    @Override
    public ResponseResult sendcode(String tel) {
        ResponseResult result = ResponseResult.SUCCESS();
        //判断手机号是否发送频繁
        Boolean bo = stringRedisTemplate.boundValueOps("SZ:" + tel).setIfAbsent("y", 1, TimeUnit.MINUTES);
        if(!bo){
            result.setSuccess(false);
            result.setMessage("短信发送过于频繁！");
            log.info("短信发送过于频繁");
            return result;
        }
        //判断短信发送是否达到上限
        String num = stringRedisTemplate.boundValueOps("PZ:" + tel).get();
        if(num!=null){
            if(Integer.valueOf(num)>10){
                result.setSuccess(false);
                result.setMessage("手机号发送已达上限！");
                log.info("手机号发送已达上限");
                return result;
            }
        }else{
            stringRedisTemplate.boundValueOps("PZ:"+tel).expire(1,TimeUnit.DAYS);
        }
        stringRedisTemplate.boundValueOps("PZ:"+tel).increment();
        //发送验证码
        int cod = (int) ((Math.random()*10)*100000);
        String code = String.valueOf(cod);
        log.info("验证码为"+code);
        stringRedisTemplate.boundValueOps("YZ:"+tel).set(code,2,TimeUnit.MINUTES);
        System.out.println("验证码是:"+code);
        //SendSms.sendSms(tel,code);
        result.setMessage("发送成功");
        return result;
    }

    @Override
    public ResponseResult zhuce(ZhuceLogin zhuceLogin) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            String code = stringRedisTemplate.boundValueOps("YZ:" +zhuceLogin.getTel()).get();
            if(code==null){
                result.setSuccess(false);
                result.setMessage("验证码已过期");
                log.info("验证码已过期");
                return result;
            }
            if(!code.equals(zhuceLogin.getCode())){
                result.setSuccess(false);
                result.setMessage("验证码输入错误");
                log.info("验证码输入错误");
                return result;
            }
            if(zhuceLogin.getType()==0){
                EbankBorrower ebankBorrower = new EbankBorrower();
                ebankBorrower.setLoginName(zhuceLogin.getLoginName());
                String s = MD5Utils.md5Encrypt32Lower(zhuceLogin.getLoginPwd());
                ebankBorrower.setLoginPwd(MD5Utils.md5Encrypt32Lower(s));
                ebankBorrower.setType(zhuceLogin.getComtype());
                ebankBorrower.setTel(zhuceLogin.getTel());
                ebankBorrower.setFullName(zhuceLogin.getFullName());
                ebankBorrower.setRegTime(new Date());
                BigDecimal bigDecimal = new BigDecimal(0);
                ebankBorrower.setBalance(bigDecimal);
                ebankBorrower.setSource(0);
                ebankBorrower.setStatus(0);
                borrowerService.saveOrUpdate(ebankBorrower);
            }
            if(zhuceLogin.getType()==1){
                EbankInvestor ebankInvestor = new EbankInvestor();
                ebankInvestor.setLoginName(zhuceLogin.getLoginName());
                String s = MD5Utils.md5Encrypt32Lower(zhuceLogin.getLoginPwd());
                ebankInvestor.setLoginPwd(MD5Utils.md5Encrypt32Lower(s));
                ebankInvestor.setTel(zhuceLogin.getTel());
                ebankInvestor.setRecoTel(zhuceLogin.getRecoTel());
                ebankInvestor.setFullName(zhuceLogin.getFullName());
                ebankInvestor.setRegTime(new Date());
                BigDecimal bigDecimal = new BigDecimal(0);
                ebankInvestor.setBalance(bigDecimal);
                ebankInvestor.setSource(0);
                ebankInvestor.setStatus(0);
                ebankInvestor.setAuthStatus(0);
                iEbankInvestorService.saveOrUpdate(ebankInvestor);
            }
            result.setMessage("注册成功");
        }catch (Exception e){
            log.error("登录失败"+e.toString());
            result.setSuccess(false);
            result.setMessage("登录异常"+e.toString());
        }
        return result;
    }

    @Override
    public ResponseResult sendemailcode(String tel) {
        ResponseResult result = ResponseResult.SUCCESS();
        //判断手机号是否发送频繁
        Boolean bo = stringRedisTemplate.boundValueOps("SE:" + tel).setIfAbsent("y", 1, TimeUnit.MINUTES);
        if(!bo){
            result.setSuccess(false);
            result.setMessage("短信发送过于频繁！");
            log.info("短信发送过于频繁");
            return result;
        }
        //判断短信发送是否达到上限
        String num = stringRedisTemplate.boundValueOps("PZ:" + tel).get();
        if(num!=null){
            if(Integer.valueOf(num)>10){
                result.setSuccess(false);
                result.setMessage("手机号发送已达上限！");
                log.info("手机号发送已达上限");
                return result;
            }
        }else{
            stringRedisTemplate.boundValueOps("PZ:"+tel).expire(1,TimeUnit.DAYS);
        }
        stringRedisTemplate.boundValueOps("PZ:"+tel).increment();
        //发送验证码
        int cod = (int) ((Math.random()*10)*100000);
        String code = String.valueOf(cod);
        log.info("验证码为"+code);
        stringRedisTemplate.boundValueOps("YE:"+tel).set(code,2,TimeUnit.MINUTES);
        System.out.println("验证码是:"+code);
        //SendSms.sendSms(tel,code);
        result.setMessage("发送成功");
        return result;
    }
}
